# coding: utf-8

from leancloud import Object
from leancloud import File
from leancloud import Query
from leancloud import LeanCloudError
from leancloud import User
from flask import request
from datetime import datetime
import time
import os
from werkzeug.utils import secure_filename
import json

class FarmerSayItem(Object):
    pass

class MFollowee(Object):
    pass


class MFollower(Object):
    pass


class DelSay():
     # 删除说说
    def delFarmerSayItem(self):
        objectId = request.json['objectId']
        print objectId
        print "******delFarmerSayItem*******"
        try:
            farmerSayItems = Query(FarmerSayItem).equal_to("objectId", objectId).descending(
                    'createdAt').find()
            for farmerSayItem in farmerSayItems:
                count = farmerSayItem.get('count')
                for i in range(1, count + 1):
                    print "img" + str(i)
                    fil = farmerSayItem.get("img" + str(i))
                    fil.destroy()
                farmerSayItem.destroy()
            data = {'code': 200, 'result': "delete success!"}
        except LeanCloudError, e:
            if e.code == 101:  # 服务端对应的 Class 还没创建
                pass
            else:
                raise e
            data = {'code': 101, 'result': "delete failed!"}
        print "delsaysay"
        return json.dumps(data, indent=1)

    # 删除说说
    def delAll(self):
        print "******delAllSayItem*******"
        try:
            print user
            farmerSayItems = Query(FarmerSayItem).descending(
                    'createdAt').find()
            for farmerSayItem in farmerSayItems:
                count = farmerSayItem.get('count')
                for i in range(1, count + 1):
                    print "img" + str(i)
                    fil = farmerSayItem.get("img" + str(i))
                    fil.destroy()
                farmerSayItem.destroy()
            data = {'code': 200, 'result': "delete success!"}
        except LeanCloudError, e:
            if e.code == 101:  # 服务端对应的 Class 还没创建
                pass
            else:
                raise e
            data = {'code': 101, 'result': "delete failed!"}

        dat = {'code': 200, 'result': "delete failed!",'data':str(data)}
        print "delsaysay"
        return json.dumps(dat, indent=1)